#ifndef ADSERVERREQUEST_H
#define ADSERVERREQUEST_H
#include <QHash>;
#include <QString>;
#include "AutoDetectParameters.h"
#include <QMessageBox>;


class AdserverRequest
{
    public:
            /// <summary>
            /// Class constructor
            /// </summary>
            AdserverRequest();

    protected:
            AutoDetectParameters *g_autodetect;
            QHash<QString, QString> parameters;
            QHash<QString, QString> custom_parameters;
            QString parameter_site;
            QString parameter_ip;
            QString parameter_userAgent;
            QString parameter_url;
            QString parameter_keywords;
            QString parameter_premium;
            QString parameter_zone;
            QString parameter_key;
            QString parameter_test;
            QString parameter_count;
            QString parameter_country;
            QString parameter_region;
            QString parameter_city;
            QString parameter_area;
            QString parameter_metro;
            QString parameter_zip;
            QString parameter_adstype;
            QString parameter_over_18;
            QString parameter_latitude;
            QString parameter_longitude;
            QString parameter_textborder;
            QString parameter_border;
            QString parameter_background;
            QString parameter_link;
            QString parameter_carrier;
            QString parameter_min_size_x;
            QString parameter_min_size_y;
            QString parameter_size_x;
            QString parameter_size_y;
            QString parameter_target;
            QString parameter_pixel;
            QString parameter_version;

            QString parameter_cpu_brand;
            QString parameter_cpu_model;
            QString parameter_cpu_frequency;

            QString parameter_os_description;
            QString parameter_os_package;
            QString parameter_os_version;

            QString parameter_motherboard_model;

            QString parameter_video_model;

            QString parameter_ram;

            QString parameter_hdd_brand;
            QString parameter_hdd_model;
            QString parameter_hdd_total;
            QString parameter_hdd_free;

            QString parameter_platform;
            QString parameter_connection_speed;
            QString parameter_udid;

            // QString adserverURL = "http://ads.mocean.mobi/ad";
            // QString adserverURL = "http://ads.mojiva.com/ad";
            QString m_adserverURL;


            QString URLEncode(QString sIn)
            {
                    QString sOut;
                    sOut = sIn;

                    return sOut;
            }

    public:

            /// <summary>
            /// Sets up autodetect parameters (if user omitted them)
            /// </summary>
            void SetupAutodetectParameters();

            /// <summary>
            /// Get url of AD server
            /// </summary>
            /// <returns>url</returns>
            QString getAdserverURL()
            {
                    return m_adserverURL;
            }

            /// <summary>
            /// Overrides the URL of ad server.
            /// </summary>
            /// <param name="adserverURL">new url</param>
            void setAdserverURL(QString adserverURL) {
                    m_adserverURL = adserverURL;
            }

            /// <summary>
            /// Add parameter to list if missing or update if present
            /// </summary>
            /// <param name="key">Param name</param>
            /// <param name="value">Param value</param>
            void AddParameter(QString key, QString value)
            {
                    if (parameters.contains(key)){
                        parameters.remove(key);
                        parameters.insert(key, value);
                    }
                    else parameters.insert(key, value);
            }

            /// <summary>
            /// Add parameter to list if missing or update if present
            /// </summary>
            /// <param name="key">Param name</param>
            /// <param name="value">Param value</param>
            void AddParameter(QString key, int value)
            {
                QString str = QString("%1").arg(value);
                if (parameters.contains(key)){
                    parameters.remove(key);
                    parameters.insert(key, str);
                }
                else parameters.insert(key, str);
            }

            /// <summary>
            /// Required.
            /// Set the id of the publisher site.
            /// </summary>
            /// <param name="site">Id of the site assigned by Adserver</param>
            /// <returns>AdserverRequest</returns>
            void setSite(QString site) {
                    if(!site.isEmpty()) {
                            AddParameter(parameter_site, site );
                    }
                    else if (parameters.contains(parameter_site)) parameters.remove(parameter_site);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set the IP address of the carrier gateway over which the device is connecting.
            /// </summary>
            /// <param name="ip">address</param>
            /// <returns>AdserverRequest</returns>
            void setIp(QString ip) {
                    if(!ip.isEmpty()) {
                            AddParameter(parameter_ip, ip);
                    }
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set the browser user agent of the device making the request.
            /// </summary>
            /// <param name="ua">User agent</param>
            /// <returns>AdserverRequest</returns>
            void setUa(QString ua) {
                    if(!ua.isEmpty()) {
                            AddParameter(parameter_userAgent, ua);
                    }
                    return;
            }

            /// <summary>
            /// Required.
            /// Set URL of site for which it is necessary to receive advertising.
            /// </summary>
            /// <param name="url">url</param>
            /// <returns>AdserverRequest</returns>
            void setUrl(QString url) {
                    if(!url.isEmpty()) {
                            AddParameter(parameter_url, url);
                    }
                    else if (parameters.contains(parameter_url)) parameters.remove(parameter_url);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Keywords to search ad delimited by commas.
            /// </summary>
            /// <param name="keywords">keywords</param>
            /// <returns>AdserverRequest</returns>
            void setKeywords(QString keywords) {
                    if(!keywords.isEmpty()) {
                            AddParameter(parameter_keywords, keywords);
                    }
                    else if (parameters.contains(parameter_keywords)) parameters.remove(parameter_keywords);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Filter by premium (PREMIUM_STATUS_NON_PREMIUM - non-premium,
            /// PREMIUM_STATUS_PREMIUM - premium only, PREMIUM_STATUS_BOTH - both).
            /// Can be used only by premium publishers.
            /// </summary>
            /// <param name="premium">premium</param>
            /// <returns>AdserverRequest</returns>
            void setPremium(int premium) {
                    if (premium >= 0)
                            AddParameter(parameter_premium, premium);
                    else if (parameters.contains(parameter_premium)) parameters.remove(parameter_premium);
                    return;
            }

            /// <summary>
            /// Required.
            /// Set the id of the zone of publisher site.
            /// </summary>
            /// <param name="zone">zone</param>
            /// <returns>AdserverRequest</returns>
            void setZone(QString zone) {
                    if(!zone.isEmpty())
                            AddParameter(parameter_zone, zone);
                    else if (parameters.contains(parameter_zone)) parameters.remove(parameter_zone);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Output format. Normal format uses key = OUTPUT_FORMAT_NORMAL.
            /// Parameter key should be set to OUTPUT_FORMAT_XML in order to use XML output
            /// and to OUTPUT_FORMAT_JSON in order to use JSON output.
            /// </summary>
            /// <param name="key">key</param>
            /// <returns>AdserverRequest</returns>
            void setKey(int key) {
                    if (key != 0)
                            AddParameter(parameter_key, key);
                    else if (parameters.contains(parameter_key)) parameters.remove(parameter_key);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Default setting is test mode where, if the ad code is properly installed,
            /// the ad response is "Test MODE".
            /// </summary>
            /// <param name="enabled">enabled</param>
            /// <returns>AdserverRequest</returns>
            void setTestModeEnabled(bool enabled) {
                    if(enabled) {
                            AddParameter(parameter_test, "1");
                    } else {
                            if (parameters.contains(parameter_test)) parameters.remove(parameter_test);
                    }
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Quantity of ads, returned by a server. Maximum value is 5.
            /// </summary>
            /// <param name="count">count</param>
            /// <returns>AdserverRequest</returns>
            void setCount(int count) {
                    if (count != 0)
                            AddParameter(parameter_count, count);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Country of visitor. See codes here (http://www.mojiva.com/docs/iso3166.csv).
            /// Will override country detected by IP.
            /// </summary>
            /// <param name="country">country</param>
            /// <returns>AdserverRequest</returns>
            void setCountry(QString country) {
                    if (!country.isEmpty())
                    {
                            AddParameter(parameter_country, country);
                    }
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Region of visitor. See codes for US and Canada here (http://www.mojiva.com/docs/iso3166_2.csv),
            /// others - here (http://www.mojiva.com/docs/fips10_4.csv).
            /// </summary>
            /// <param name="region">region</param>
            /// <returns>AdserverRequest</returns>
            void setRegion(QString region)
            {
                    if(!region.isEmpty()) {
                            AddParameter(parameter_region, region);
                    }
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Type of advertisement (ADS_TYPE_TEXT_ONLY - text only,
            /// ADS_TYPE_IMAGES_ONLY - image only, ADS_TYPE_TEXT_AND_IMAGES - image and text,
            /// ADS_TYPE_SMS - SMS ad). SMS will be returned in XML.
            /// </summary>
            /// <param name="adstype">type</param>
            /// <returns>AdserverRequest</returns>
            void setAdstype(int adstype)
            {
                    if (adstype != 0)
                            AddParameter(parameter_adstype, adstype);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Filter by ad over 18 content (OVER_18_TYPE_DENY - deny over 18 content ,
            /// OVER_18_TYPE_ONLY - only over 18 content, OVER_18_TYPE_ALL - allow all ads
            /// including over 18 content).
            /// </summary>
            /// <param name="over18">over18</param>
            /// <returns>AdserverRequest</returns>
            void setOver18(int over18)
            {
                    if (over18 != 0)
                            AddParameter(parameter_over_18, over18);
                    else if (parameters.contains(parameter_over_18)) parameters.remove(parameter_over_18);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Latitude.
            /// </summary>
            /// <param name="latitude">latitude</param>
            /// <returns>AdserverRequest</returns>
            void setLatitude(QString latitude)
            {
                    if(!latitude.isEmpty())
                            AddParameter(parameter_latitude, latitude);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Longitude.
            /// </summary>
            /// <param name="longitude">longitude</param>
            /// <returns>AdserverRequest</returns>
            void setLongitude(QString longitude)
            {
                    if(!longitude.isEmpty())
                            AddParameter(parameter_longitude, longitude);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Show borders around text ads.
            /// </summary>
            /// <param name="enabled">show or not</param>
            /// <returns>AdserverRequest</returns>
            void setTextborderEnabled(bool enabled)
            {
                    if(enabled)
                            AddParameter(parameter_textborder, "1");
                    else if (parameters.contains(parameter_textborder)) parameters.remove(parameter_textborder);
    //		} else {
    //			AddParameter(parameter_textborder, "0");
    //		}
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Borders color.
            /// </summary>
            /// <param name="paramBorder">color</param>
            /// <returns>AdserverRequest</returns>
            void setParamBorder(QString paramBorder)
            {
                    if(!paramBorder.isEmpty()) {
                            AddParameter(parameter_border, paramBorder);
                    }
                    else if (parameters.contains(parameter_border)) parameters.remove(parameter_border);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Background color in borders.
            /// </summary>
            /// <param name="paramBG">color</param>
            /// <returns>AdserverRequest</returns>
            void setParamBG(QString paramBG)
            {
                    if(!paramBG.isEmpty()) {
                            AddParameter(parameter_background, paramBG);
                    }
                    else if (parameters.contains(parameter_background)) parameters.remove(parameter_background);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Text color.
            /// </summary>
            /// <param name="paramLINK">color</param>
            /// <returns>AdserverRequest</returns>
            void setParamLINK(QString paramLINK)
            {
                    if(!paramLINK.isEmpty()) {
                            AddParameter(parameter_link, paramLINK);
                    }
                    else if (parameters.contains(parameter_link)) parameters.remove(parameter_link);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set Carrier name.
            /// </summary>
            /// <param name="carrier">name</param>
            /// <returns>AdserverRequest</returns>
            void setCarrier(QString carrier)
            {
                    if(!carrier.isEmpty()) {
                            AddParameter(parameter_carrier, carrier);
                    }
                    else if (parameters.contains(parameter_carrier)) parameters.remove(parameter_carrier);
                    return;
            }

            void set_min_size_x(int val)
            {
                    if (val != 0)
                            AddParameter(parameter_min_size_x, val);
                    else if (parameters.contains(parameter_min_size_x)) parameters.remove(parameter_min_size_x);
            }

            void set_min_size_y(int val)
            {
                    if (val != 0)
                            AddParameter(parameter_min_size_y, val);
                    else if (parameters.contains(parameter_min_size_y)) parameters.remove(parameter_min_size_y);
            }

            void set_size_x(int val)
            {
                    if (val != 0)
                            AddParameter(parameter_size_x, val);
                    else if (parameters.contains(parameter_size_x)) parameters.remove(parameter_size_x);
            }

            void set_size_y(int val)
            {
                    if (val != 0)
                            AddParameter(parameter_size_y, val);
                    else if (parameters.contains(parameter_size_y)) parameters.remove(parameter_size_y);
            }

            /// <summary>
            /// Optional.
            /// Set Target attribute for "a href" (TARGET_BLANK - open the linked document
            /// in a new window, TARGET_SELF - open the linked document in the same frame,
            /// TARGET_PARENT - open the linked document in the parent frameset,
            /// TARGET_TOP - open the linked document in the full body of the window)
            /// </summary>
            /// <param name="target">target</param>
            /// <returns>AdserverRequest</returns>
            void setTarget(QString target)
            {
                    if(!target.isEmpty()) {
                            AddParameter(parameter_target, target);
                    }
                    else if (parameters.contains(parameter_target)) parameters.remove(parameter_target);
                    return;
            }

            /// <summary>
            /// Optional.
            /// Set for return redirect to image for ad (or tracking pixel 1x1) directly
            /// (instead of html response).
            /// </summary>
            /// <param name="enabled">enabled</param>
            /// <returns>AdserverRequest</returns>
            void setPixelModeEnabled(bool enabled)
            {
                    if(enabled) {
                            AddParameter(parameter_pixel, "1");
                    } else {
                            if (parameters.contains(parameter_pixel)) parameters.remove(parameter_pixel);
                    }
                    return;
            }

            void setParam( QString strName, QString strValue )
            {
                    if (!strValue.isEmpty())
                    {
                            AddParameter(strName, strValue);
                    }
                    else
                    {
                            if (parameters.contains(strName)) parameters.remove(strName);
                    }
            }

            void setCpuBrand(QString param)
            {
                    setParam(parameter_cpu_brand, param );
            }

            void setCpuModel(QString param)
            {
                    setParam(parameter_cpu_model, param );
            }

            void setCpuFrequency(QString param)
            {
                    setParam(parameter_cpu_frequency, param );
            }

            void setOsDescription(QString param)
            {
                    setParam(parameter_os_description, param );
            }

            void setOsPackage(QString param)
            {
                    setParam(parameter_os_package, param );
            }

            void setOsVersion(QString param)
            {
                    setParam(parameter_os_version, param );
            }

            void setMotherboardModel(QString param)
            {
                    setParam(parameter_motherboard_model, param );
            }

            void setVideoModel(QString param)
            {
                    setParam(parameter_video_model, param );
            }

            void setRam(QString param)
            {
                    setParam(parameter_ram, param );
            }

            void setHddBrand(QString param)
            {
                    setParam(parameter_hdd_brand, param );
            }

            void setHddModel(QString param)
            {
                    setParam(parameter_hdd_model, param );
            }

            void setHddTotal(QString param)
            {
                    setParam(parameter_hdd_total, param );
            }

            void setHddFree(QString param)
            {
                    setParam(parameter_hdd_free, param );
            }

            void setPlatform(QString param)
            {
                    setParam(parameter_platform, param );
            }

            QString getSite() {
                    return (parameters.contains(parameter_site)) ? parameters.value(parameter_site) : "";
            }



            QString getUa() {
                    if ( (parameters.contains(parameter_userAgent))
                            && !parameters.value(parameter_userAgent).isEmpty()) return parameters.value(parameter_userAgent);
                    else if (!g_autodetect->m_ua.isEmpty())
                    {
                        QString s = QString("%1").arg(g_autodetect->m_ua);
                        return s;
                    }

                    return "";
            }

            QString getUrl() {
                    return (parameters.contains(parameter_url)) ? parameters.value(parameter_url) : "";
            }

            QString getKeywords() {
                    return (parameters.contains(parameter_keywords)) ? parameters.value(parameter_keywords) : "";
            }

            int getPremium() {
                    if (parameters.contains(parameter_premium))
                    {
                            bool ok;
                            return parameters.value(parameter_premium).toInt() + 1;
                    }

                    return 0;
            }

            QString getZone() {
                    return (parameters.contains(parameter_zone)) ? parameters.value(parameter_zone) : "";
            }

            int getKey() {
                    if (parameters.contains(parameter_key))
                    {
                            return parameters.value(parameter_key).toInt();
                    }

                    return 0;
            }

            int getCount() {
                    if (parameters.contains(parameter_count))
                    {
                            return parameters.value(parameter_count).toInt();
                    }

                    return 0;
            }


            QString getRegion() {
                    return (parameters.contains(parameter_region)) ? parameters.value(parameter_region) : "";
            }

            int getAdstype() {
                    if (parameters.contains(parameter_adstype) != 1){
                            return parameters.value(parameter_adstype).toInt();
                    }

                    return 0;
            }

            int getOver18() {
                    if (parameters.contains(parameter_over_18))
                    {
                            return parameters.value(parameter_over_18).toInt();
                    }

                    return 0;
            }

            QString getLatitude() {

                    if ( (parameters.contains(parameter_latitude))
                            && !parameters.value(parameter_latitude).isEmpty() )
                            return parameters.value(parameter_latitude);
                    else if (g_autodetect->m_dLat != 0)
                    {
                        QString s = QString("%1").arg(g_autodetect->m_dLat);
                        return s;
                    }
                    return "";
            }

            QString getLongitude() {
                    if ( (parameters.contains(parameter_longitude)) && !parameters.value(parameter_longitude).isEmpty())
                            return parameters.value(parameter_longitude);
                    else if (g_autodetect->m_dLon != 0)
                    {
                            QString s = QString("%1").arg(g_autodetect->m_dLon);
                            return s;
                    }

                    return "";
            }

            QString getParamBorder() {
                    return (parameters.contains(parameter_border)) ? parameters.value(parameter_border) : QString("");
            }

            QString getParamBG() {
                    return (parameters.contains(parameter_background)) ? parameters.value(parameter_background) : QString("");
            }

            QString getParamLINK() {
                    return (parameters.contains(parameter_link)) ? parameters.value(parameter_link) : QString("");
            }

            QString getCarrier() {
                    if ( (parameters.contains(parameter_carrier)) && !parameters.value(parameter_carrier).isEmpty())
                            return parameters.value(parameter_carrier);

                    return "";
            }

            int get_min_size_x()
            {
                    return parameters.value(parameter_min_size_x).toInt();
            }

            int get_min_size_y()
            {
                    return parameters.value(parameter_min_size_y).toInt();
            }

            int get_size_x()
            {
                    return parameters.value(parameter_size_x).toInt();
            }

            int get_size_y()
            {
                    return parameters.value(parameter_size_y).toInt();
            }

            int getTarget() {
                    return parameters.value(parameter_target).toInt();
            }


            QString getConnectionSpeed()
            {
                    if (!g_autodetect->m_strConnectionSpeed.isEmpty())
                    {
                            return g_autodetect->m_strConnectionSpeed;
                    }

                    return "";
            }

            QString getUdid()
            {
                    return g_autodetect->m_strDeviceID;
            }

            QString getCity()
            {
                    return (parameters.contains(parameter_city)) ? parameters.value(parameter_city) : "";
            }

            void setCity( QString val)
            {
                    if(!val.isEmpty()) {
                            AddParameter(parameter_city, val);
                    }
            }

            QString getArea()
            {
                    return (parameters.contains(parameter_area)) ? parameters.value(parameter_area) : "";
            }

            void setArea( QString val)
            {
                    if(!val.isEmpty()) {
                            AddParameter(parameter_area, val);
                    }
            }

            QString getMetro()
            {
                    return (parameters.contains(parameter_metro)) ? parameters.value(parameter_metro) : "";
            }

            void setMetro( QString val)
            {
                    if(!val.isEmpty()) {
                            AddParameter(parameter_metro, val);
                    }
            }

            QString getZIP()
            {
                    return (parameters.contains(parameter_zip)) ? parameters.value(parameter_zip) : "";
            }

            void setZIP( QString val)
            {
                    if(!val.isEmpty()) {
                            AddParameter(parameter_zip, val);
                    }
            }

            /**
            * Creates URL with given parameters.
            */
            QString CreateURL();

            friend class AdViewControl;
};


#endif // ADSERVERREQUEST_H
